var columnNumber = 3;

/*=================================页面事件============================================================*/
//页面的load方法
function pageLoad(){
	addRow(tbMoreImgs);
	addRow(tbMoreImgs);
	if(bXmlHttpSupport) {
    	var sUrl = 'loadSettingsPage.action';
    	var params = "category=系统选项&title=焦点图管理&item=Focus&setKey=Home";
        var request = new XMLHttpRequest();
        request.onreadystatechange = function() {
        	if(request.readyState == 4) {
        		//alert(request.responseText);
            	querySettings();
			}
    	};
    	request.open('POST', sUrl);
    	request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
    	request.send(params);
    }
}

/*=================================数据读取============================================================*/

//根据item和setKey查询对应的settings记录
function querySettings(){
	if(bXmlHttpSupport) {
    	var sUrl = 'querySettings.action';
    	var params = "item=Focus&setKey=Home";
        var request = new XMLHttpRequest();
        request.onreadystatechange = function() {
        	if(request.readyState == 4) {
        		//alert(request.responseText);
            	var oRequest = eval('(' + request.responseText + ')');
            	putSettingsIntoPage(oRequest);
			}
    	};
    	request.open('POST', sUrl);
    	request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
    	request.send(params);
    }
}

//将查到的结果放入对应的位置
function putSettingsIntoPage(oRequest){
	var settings = oRequest.settings;
	var imgLen = settings.settingsImg.length;
	if (imgLen == 0){
		return;
	}
	var rowObj;
	var tbCurrentImgs = document.getElementById("tbCurrentImgs");
	//计算要生成的行数
	var lines = Math.ceil(imgLen / columnNumber);
	for ( var i = 0; i < lines; i++){
		rowObj = document.createElement("tr");
		for (var j = i * columnNumber; j < (i + 1) * columnNumber; j++){
			var html;
			if ( j > imgLen -1){
				html = "没有图片";
			}else{
				html = 	"<div id='tdDIV'>" +
						"<div id='tdDIV1'><img src='loadSettingsImgByID.action?imgID=" + settings.settingsImg[j].settingsImgID+"'/></div>" +
						"<div id='tdDIV2'><input type='button' id='delImg' value='删除' onclick='delExistedImg(" + settings.settingsImg[j].settingsImgID +",this);'/></div>" +
						"</div>";
			}
			addOneTD(rowObj, html, "");
			tbCurrentImgs.appendChild(rowObj);
		}
	}
}

/*=================================与后台交互============================================================*/
//提交修改
function modifySettings(){
	if (!validateImgs()){
		alert("如果您点击了”添加图片“，就要选择图片，否则请点击”删除“！");
		return;
	}
	var imgs = document.getElementsByTagName("img");
	if (imgs.length != 6){
		alert("焦点图必须为6张，请确保6张再保存。");
		return;
	}
	var frmModifySettingsImg = document.getElementById("frmModifySettingsImg");
	frmModifySettingsImg.submit();
}

//点击删除按钮后，将该位置换回添加图片按钮，并访问后台，将imgID加入待删除列表
function delExistedImg(imgID, delBtn){
	if(bXmlHttpSupport) {
    	var sUrl = 'delOneSettingsImg.action';
    	var params = "imgID=" + imgID;
        var request = new XMLHttpRequest();
        request.onreadystatechange = function() {
        	if(request.readyState == 4) {
        		var currentDIV = delBtn.parentNode.parentNode;
        		var html = 	"没有图片";
        		currentDIV.innerHTML = html;
			}
    	};
    	request.open('POST', sUrl);
    	request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
    	request.send(params);
    }
}